import time

from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait

from Pages import BasePage

class AdminPage1(BasePage.Action):
    def __init__(self):
        #订单管理按钮
        self.orders_xpath = (By.XPATH, "//div/span[text()='订单管理']")
        #机票订单按钮
        self.flightOrders_xpath = (By.XPATH, "//li[@class='el-menu-item']//*[text()='机票订单']")
        self.hotelOrders_xpath = (By.XPATH, "//li[@class='el-menu-item']//*[text()='酒店订单']")
        self.visaOrders_xpath = (By.XPATH, "//li[@class='el-menu-item']//*[text()='签证订单']")
        #是否测试单checkbox
        self.isTestOrders_xpath = (By.XPATH, "//span[text()='过滤测试单']")
        #订单号输入框
        self.flightOrderNoInput_xpath = (By.XPATH, "//input[contains(@placeholder, '订单号/渠道订单号')]")
        self.hotelOrderNoInput_xpath = (By.XPATH, "//input[@placeholder= '订单号']")
        self.visaOrderNoInput_xpath = (By.XPATH, "//input[@placeholder= '订单号']")
        #查询按钮
        self.searchButton_xpath = (By.XPATH, "//button/span[contains(text(),'查询')]")


    def goToFlightOrder(self):
        self.smart_click(*self.orders_xpath)
        # 等待并点击机票订单菜单
        self.wait_for_element_clickable(*self.flightOrders_xpath).click()
        # 等待订单查询页面加载完成
        self.wait_for_element_visible(*self.flightOrderNoInput_xpath)

    def goToHotelOrder(self):
        time.sleep(3)
        self.smart_click(*self.orders_xpath)
        time.sleep(2)
        self.smart_click(*self.hotelOrders_xpath)
        time.sleep(2)

    def goToVisaOrder(self):
        self.smart_click(*self.orders_xpath)
        time.sleep(1)
        #滚动至元素可见
        self.scroll_into_view(self.find_element(*self.visaOrders_xpath))
        time.sleep(2)
        self.smart_click(*self.visaOrders_xpath)
        time.sleep(2)

    def selectOrder(self, orderNo: object) -> object:
        # 根据订单号类型选择对应操作
        if orderNo[0:2] in ['DF', 'IF']:
            self.wait_for_element_clickable(*self.isTestOrders_xpath).click()
            self.wait_for_element(*self.flightOrderNoInput_xpath).send_keys(orderNo)
        elif orderNo[0:2] == 'HO':
            self.wait_for_element_clickable(*self.isTestOrders_xpath).click()
            self.wait_for_element(*self.hotelOrderNoInput_xpath).send_keys(orderNo)
        elif orderNo[0:2] == 'QZ':
            self.wait_for_element_clickable(*self.isTestOrders_xpath).click()
            self.wait_for_element(*self.visaOrderNoInput_xpath).send_keys(orderNo)

        # 点击查询按钮
        self.smart_click(*self.searchButton_xpath)

        # 等待查询结果出现
        aimedOrder_xpath = (By.XPATH, "//span[contains(text(),'%s')]" % orderNo)
        self.wait_for_element_clickable(*aimedOrder_xpath).click()

        # 等待新窗口打开并切换
        WebDriverWait(self._driver, 10).until(
            lambda driver: len(driver.window_handles) > 1
        )
        self.switch_window()
